namespace cnVery.Cms.UI
{
    using cnVery.Cms.Kernel;
    using System;
    using System.Data;
    using System.Web.UI.WebControls;

    public class ShoppingCart : User_Main
    {
        private DataTable cart;
        protected Repeater hotproducts;
        protected DataGrid list;
        protected decimal subtotal;
        protected Repeater vouchproducts;

        private void BindCart()
        {
            this.cart = this.GetDataSource();
            this.list.DataSource = this.cart;
            this.list.DataBind();
            if (this.cart != null)
            {
                for (int i = 0; i < this.cart.Rows.Count; i++)
                {
                    this.subtotal += (decimal) this.cart.Rows[i]["Total"];
                }
            }
        }

        private DataTable CreateDataSource()
        {
            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("Price", typeof(decimal));
            table.Columns.Add("Total", typeof(decimal));
            table.Columns.Add("Count", typeof(int));
            table.Columns.Add("Deleted", typeof(bool));
            return table;
        }

        private DataTable GetDataSource()
        {
            return (DataTable) this.Session["cart"];
        }

        private void InitializeComponent()
        {
            base.Load += new EventHandler(this.Page_Load);
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
        }

        protected void onupdate(object sender, EventArgs e)
        {
            this.cart = this.CreateDataSource();
            try
            {
                for (int i = 0; i < this.list.Items.Count; i++)
                {
                    CheckBox box = (CheckBox) this.list.Items[i].FindControl("Deleted");
                    Label label = (Label) this.list.Items[i].FindControl("Name");
                    Label label2 = (Label) this.list.Items[i].FindControl("Price");
                    Label label3 = (Label) this.list.Items[i].FindControl("Total");
                    TextBox box2 = (TextBox) this.list.Items[i].FindControl("ID");
                    TextBox box3 = (TextBox) this.list.Items[i].FindControl("Count");
                    if (!box.Checked)
                    {
                        DataRow row = this.cart.NewRow();
                        row["ID"] = int.Parse(box2.Text);
                        row["Name"] = label.Text;
                        row["Price"] = decimal.Parse(label2.Text);
                        row["Count"] = int.Parse(box3.Text);
                        row["Total"] = ((int) row["Count"]) * ((decimal) row["Price"]);
                        row["Deleted"] = false;
                        this.cart.Rows.Add(row);
                    }
                }
                this.cart.AcceptChanges();
                this.Session["cart"] = this.cart;
                this.Session.Timeout = 0xe10;
                base.Response.Redirect("shoppingcart.aspx");
            }
            catch (Exception exception)
            {
                this.Alert(exception.Message);
            }
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.BindCart();
            }
        }
    }
}

